package cn.com.dhcc.app.pub.core.listener;

import javax.servlet.ServletContextEvent;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.KernelParam;
import cn.com.dhcc.turbo.service.transport.FtpService;

/**
 * 管理内置的FTP生命周期
 * 项目启动时候启动内置的FTPServer并初始化对应的用户
 * 项目关闭后停止FTP服务
 */
public class FtpInitListener implements AppInitListener {

	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam) throws InitException {
		contextEvent.getServletContext().log("开始启动内嵌FTP 服务...");
		FtpService ftpService = CM.getBean(FtpService.class);
		ftpService.initEmbFtp();
		contextEvent.getServletContext().log("内嵌FTP服务启动成功. listenning on "+AppConst.installIp+"(映射IP{"+AppConst.mappingIp+"}):"+AppConst.EMBFTP.EMBFTP_PORT+(KernelParam.encryptFtpUsessl?",ssl on.":""));
	}

	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam) throws Exception {
		contextEvent.getServletContext().log("Stopping embeded FTP Server...");
		CM.getBean(FtpService.class).stopFtpServer(contextEvent.getServletContext());
		contextEvent.getServletContext().log("embeded FTP Server Stopped.");
	}
}
